$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'exam/exam/list',
        datatype: "json",
        colModel: [
			{ label: '日期', name: 'examDate', width: 80, sortable: false, formatter:'date',formatoptions:{newformat:'Y-m-d'}},
			{ label: '编号', name: 'studentCode', width: 80, sortable: false},
			{ label: '姓名', name: 'name', width: 80, sortable: false },
			{ label: '性别', name: 'sex', width: 80, sortable: false,formatter: "select", editoptions:{value:"0:女;1:男"} },
			{ label: '年龄', name: 'age', width: 80 },
			{ label: '身高(cm)', name: 'height', width: 80 },
			{ label: '体重(kg)', name: 'weight', width: 80 },
			{ label: 'BMI', name: 'bmi', width: 80 },
			{ label: '是否合格', name: 'shapeLevel', width: 80, formatter: "select", editoptions:{value:"0:不合格;1:合格"}  },
			{ label: '引体向上', name: 'leadUp', width: 80, sortable: false },
			{ label: '曲臂悬垂(秒)', name: 'crankArm', width: 80, sortable: false },
			{ label: '成绩', name: 'crankArmScore', width: 50, sortable: false },
			{ label: '俯卧撑', name: 'pushUp', width: 80, sortable: false },
			{ label: '成绩', name: 'pushUpScore', width: 50 , sortable: false},
			{ label: '仰卧起坐', name: 'sitUp', width: 80 , sortable: false},
			{ label: '成绩', name: 'sitUpScore', width: 50 , sortable: false},
			{ label: '蛇形跑(秒)', name: 'snakeRun', width: 80, sortable: false },
			{ label: '成绩', name: 'snakeRunScore', width: 50, sortable: false },
			{ label: '中长跑(分.秒)', name: 'middleRun', width: 80, sortable: false },
			{ label: '成绩', name: 'middleRunScore', width: 50, sortable: false },
			{ label: '成绩总评', name: 'totalScore', width: 60, sortable: false },
			{ label: '等级评定', name: 'level', width: 80, sortable: false, formatter: "select", editoptions:{value:"1:不及格;2:及格;3:良好;4:优秀;5:特三级;6:特二级;7:特一级"}  }
		],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25,
		shrinkToFit:false,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit"
        }
        // ,
        // gridComplete:function(){
        // 	//隐藏grid底部滚动条
        // 	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        // }
    });

	new AjaxUpload('#upload', {
		action: baseURL + "exam/exam/upload/1",
		name: 'file',
		autoSubmit:true,
		responseType:"json",
		onSubmit:function(file, extension){
			// 校验文件格式
			if (!(extension && /^(xlsx|txt)$/.test(extension.toLowerCase()))){
			    alert('只支持xls,xlsx格式的文档！');
			    return false;
			}
		},
		onComplete : function(file, r){
			if(r.code == 0){
				alert(r.msg);
				vm.reload();
			}else{
				alert(r.msg);
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            name: null,
			studentCode: null,
			examDate: null,
			shapeLevel: null,
			level: null
        },
		showList: true,
		title: null,
		exam: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增人员考核";
			vm.exam = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改人员考核";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.exam.id == null ? "exam/exam/save" : "exam/exam/update";
			vm.exam.examDate = $('#examDateId').val();
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.exam),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "exam/exam/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "exam/exam/info/"+id, function(r){
                vm.exam = r.exam;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name, 'studentCode': vm.q.studentCode, 'examDate': vm.q.examDate, 'shapeLevel': vm.q.shapeLevel, 'level': vm.q.level},
                page:page
            }).trigger("reloadGrid");
		},
		downloadTemp: function() {
			var url = baseURL + "exam/exam/downloadExTemp";
			window.location.href = url;
		},
		downloadEx: function () {
			var url = baseURL + "exam/exam/download";
			var params = '';
			if (vm.q.name) {
                params = params + "&name=" + vm.q.name;
            }
            if (vm.q.studentCode) {
                params = params + "&studentCode=" + vm.q.studentCode;
            }
			if (vm.q.shapeLevel) {
				params = params + "&shapeLevel=" + vm.q.shapeLevel;
			}
			if (vm.q.level) {
				params = params + "&level=" + vm.q.level;
			}
            if (params.length > 1) {
				url  = url + "?" + params.substring(1, params.length);
			}
			// alert(url);
            window.location.href = url;
		}
	}
});

$('#examDateId').datepicker({
	language: 'zh-CN',
	format: 'yyyy-mm-dd'
});
// $('#qExamDateId').datepicker({
// 	language: 'zh-CN',
// 	format: 'yyyy-mm-dd'
// });